/**
 * Copyright 2009 Erick Armbrust 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */

package tapioca.dt;

import java.util.Set;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.repackaged.com.google.common.collect.Sets;

import tapioca.testing.AppEngineTestCase;

/**
 * Tests for the {@link PutRequest} class.
 *
 * @author armbrust@gmail.com (Erick Armbrust)
 */
public class PutRequestTest extends AppEngineTestCase {

  public void testEquals() {
    Key k1 = KeyFactory.createKey("foo", 1L);
    Key k2 = KeyFactory.createKey("foo", 1L);
    
    PutRequest p1 = new PutRequest(k1, k2, true);
    PutRequest p2 = new PutRequest(k1, k2, true);
    PutRequest p3 = new PutRequest(k1, k2, false);
    
    assertEquals(p1, p1);
    assertEquals(p1, p2);
    assertFalse(p1.equals(p3) || p2.equals(p3));
    assertFalse(p1.equals(new Object()));
  }
  
  public void testHashCode() {
    Key k1 = KeyFactory.createKey("foo", 1L);
    Key k2 = KeyFactory.createKey("foo", 1L);
    
    PutRequest p1 = new PutRequest(k1, k2, true);
    PutRequest p2 = new PutRequest(k1, k2, true);
    PutRequest p3 = new PutRequest(k1, k2, false);
    
    assertEquals(p1.hashCode(), p1.hashCode());
    assertEquals(p1.hashCode(), p2.hashCode());
    assertFalse(p1.hashCode() == p3.hashCode() || p2.hashCode() == p3.hashCode());
    
    Set<PutRequest> set = Sets.newHashSet(p1, p2, p3);
    assertEquals(2, set.size());
    assertTrue(set.contains(p1));
    assertTrue(set.contains(p2));
    assertTrue(set.contains(p3));
  }  
  
}
